package com.xianzhiOA.service.impl;

import com.github.pagehelper.PageHelper;
import com.xianzhiOA.bean.XzSchedule;
import com.xianzhiOA.dao.XzScheduleMapper;
import com.xianzhiOA.service.XzScheduleService;
import com.xianzhiOA.service.XzWorkService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *ScheduleServiceImpl
 *
 * @author 甘汝雷
 * @create 2017-04-19 10:17
 **/
@Service("XzScheduleService")
public class XzScheduleServiceImpl implements XzScheduleService{
    @Autowired
    private XzScheduleMapper scheduleMapper;
    @Autowired
    private XzWorkService workService;
    @Override
    public PageBean<XzSchedule> selScheduleAll(XzSchedule schedule, int page, int rows, Date beginDate, Date endDate, Date begin) {
        PageHelper.startPage(page, rows);
        Map map = myMap(schedule);
        if (beginDate != null) map.put("beginDate",beginDate);
        if (endDate != null) map.put("endDate",endDate);
        if (begin != null) map.putAll(workService.getWeekByDate(begin));
        List<XzSchedule> scheduleList = scheduleMapper.selAll(map);
        PageBean<XzSchedule> pb = new PageBean<>(scheduleList);
        return pb;
    }

    //查询所有进度信息
    @Override
    public List<XzSchedule> selScheduleAll(XzSchedule schedule){
        return scheduleMapper.selAll(myMap(schedule));
    }
    //查询所有进度信息
    @Override
    public List<XzSchedule> selScheduleAll(Map map){
        return scheduleMapper.selAll(map);
    }

    //把schedule转换成map
    public Map myMap(XzSchedule schedule) {
        Map map = new HashMap<>();
        map.put("scheduleId",schedule.getScheduleId());
        map.put("scheduleName",schedule.getScheduleName());
        map.put("scheduleProject",schedule.getScheduleProject());
        map.put("schedulePlan", schedule.getSchedulePlan());
        map.put("scheduleActual",schedule.getScheduleActual());
        map.put("scheduleCause",schedule.getScheduleCause());
        map.put("scheduleImprove",schedule.getScheduleImprove());
        map.put("scheduleTime",schedule.getScheduleTime());
        map.put("empId",schedule.getEmpId());
        map.put("field1",schedule.getField1());
        map.put("field2",schedule.getField2());
        map.put("field3",schedule.getField3());
        map.put("isdelete",schedule.getIsdelete());
        map.put("scheduleEmpName",schedule.getScheduleEmpName());
        map.put("projectName",schedule.getProjectName());
        return map;
    }

    //通过id查询进度信息
    @Override
    public XzSchedule selByScheduleId(Long scheduleId) {
        return scheduleMapper.selById(scheduleId);
    }

    //添加进度信息
    @Override
    public int addSchedule(XzSchedule schedule) {
        return scheduleMapper.insert(schedule);
    }

    //删除进度信息
    @Override
    public int delete(Long scheduleId){
        return scheduleMapper.delete(scheduleId);
    }

    //修改进度信息
    @Override
    public int update(XzSchedule schedule){
        return scheduleMapper.update(myMap(schedule));
    }

    //伪删除
    @Override
    public int isdelete(Long scheduleId) {
        Map map = new HashMap();
        map.put("scheduleId",scheduleId);
        map.put("isdelete",1);
        return scheduleMapper.update(map);
    }
}
